<?php
ob_start();
header('Content-type: text/html;charset=utf-8');
/*
 此文件主要功能如下：
 1.在数据库中添加新记录
 2.返回新加记录信息。JSON格式
 3.创建上传目录
 此文件主要在数据库中添加新的记录并返回文件信息
 如果存在则在数据库中添加一条相同记录。返回添加的信息
 如果不存在，则向数据库中添加一条记录。并返回此记录ID
 控件每次计算完文件MD5时都将向信息上传到此文件中
 @更新记录：
 2014-08-12 完成逻辑。
 2017-07-11 优化
 */
require '../../../vendor/autoload.php';

use biz\PathBuilderUuid;
use biz\up6_biz_event;
use biz\WebSafe;
use database\DBFile;
use model\FileInf;
use store\StorageType;
use utils\ConfigReader;
use utils\CryptoTool;
use utils\PathTool;
use utils\WebBase;

$wb = new WebBase();

$pid      = $wb->queryString("pid");
$pidRoot  = $wb->queryString("pidRoot");
$md5      = $_GET["md5"];
$id       = $_GET["id"];
$uid      = $_GET["uid"];
$lenLoc   = $_GET["lenLoc"];//数字化的文件大小
$sizeLoc  = $_GET["sizeLoc"];//格式化的文件大小
$sizeLoc  = str_replace("+", " ", $sizeLoc);
$blockSize= $wb->reqString("blockSize", "5242880");
$token    = $_GET["token"];
$callback = $_GET["callback"];//jsonp
$pathLoc  = $_GET["pathLoc"];
$pathLoc  = PathTool::urldecode_path($pathLoc);

if(    empty($md5)
    || strlen($uid)<1
    || empty($sizeLoc))
{
    echo $callback . "({\"value\":null})";
    die();
}

$ext = PathTool::getExtention($pathLoc);
$fileSvr = new FileInf();
$fileSvr->uid = $uid;
$fileSvr->id = $id;
$fileSvr->pid = $pid;
$fileSvr->pidRoot = $pidRoot;
$fileSvr->fdChild = false;
$fileSvr->fdTask = false;
$fileSvr->nameLoc = PathTool::getName($pathLoc);
$fileSvr->pathLoc = $pathLoc;
$fileSvr->nameSvr = $fileSvr->nameLoc;
$fileSvr->blockSize = intval($blockSize);//块大小
$fileSvr->blockSizeSec = $wb->reqInt("blockSizeSec");
$fileSvr->lenLoc = intval($lenLoc);
$fileSvr->lenLocSec = $wb->reqInt("lenLocSec");
$fileSvr->sizeLoc = $sizeLoc;
$fileSvr->deleted = false;
$fileSvr->md5 = $md5;
$fileSvr->encrypt = ConfigReader::storageEncrypt();//存储加密
$fileSvr->encryptAgo = $wb->reqString("encryptAgo");//加密算法

$ws = new WebSafe();
$ret = $ws->validToken($token,$fileSvr);
if(!$ret)
{
    echo $callback . "({\"value\":\"0\",\"ret\":false,\"msg\":\"token error\"})";
    die();
}

//生成路径
$pb = new PathBuilderUuid();
$fileSvr->pathSvr = $pb->genFile($uid,$fileSvr);
$fileSvr->pathSvr = str_replace("\\", "/", $fileSvr->pathSvr);

//创建文件
$fr = ConfigReader::blockWriter();
try
{
    $newID = $fr->make($fileSvr);
    if($fr->storage == StorageType::FastDFS)
    {
        $fileSvr->object_id = $newID;
        if(empty($newID))
        {
            $out = array("value"=>0,"ret"=>false,"error"=>"FastDFS create error");
            exit($callback."(".json_encode($out).")");
            return;
        }
    }
    else if($fr->storage == StorageType::Minio)
    {
        $fileSvr->object_id = $newID;
        if(empty($newID))
        {
            $out = array("value"=>0,"ret"=>false,"error"=>"minio create file error");
            exit($callback."(".json_encode($out).")");
            return;
        }
    }

}
catch(Exception $e)
{
    $out = array("value"=>0,"ret"=>false,"error"=>$e->getMessage()." pathSvr:".$fileSvr->pathSvr);
    exit($callback."(".json_encode($out).")");
    return;
}

//触发事件
up6_biz_event::file_create($fileSvr);
    

//将路径转换成相对路径
$fileSvr->pathSvr = $pb->absToRel($fileSvr->pathSvr);

$json = json_encode($fileSvr,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE);//

$json = urlencode($json);
$json = str_replace("+","%20",$json);
$json = $callback . "({'value':'$json','ret':true})";//返回jsonp格式数据。
echo $json;
header('Content-Length: ' . ob_get_length());
header("content-type:text/html;charset=utf-8");  //设置编码
?>